package com.zzz.codegen.util;

import cn.hutool.core.util.StrUtil;
import com.zzz.codegen.config.GroupConfig;
import com.zzz.codegen.config.TemplateConfig;
import com.zzz.codegen.entity.TableVO;

import java.io.File;

public class CodegenUtil extends StrUtil {

    public static String getEntityJavaFileName(GroupConfig group, TableVO table) {
        String entityJavaFileName = table.getTableName();
        String tablePrefix = group.getTablePrefix();
        if (isNotBlank(tablePrefix)) {
            String[] tablePrefixes = tablePrefix.split(",");
            for (String prefix : tablePrefixes) {
                String trimPrefix = prefix.trim();
                if (startWith(entityJavaFileName, trimPrefix)) {
                    entityJavaFileName = entityJavaFileName.substring(trimPrefix.length());
                    break;
                }
            }
        }
        return upperFirst(toCamelCase(entityJavaFileName));
    }

    public static File buildJavaFile(TemplateConfig templateConfig) {
        GroupConfig group = templateConfig.getGroup();
        String filePath = isBlank(group.getSourceDir()) ? System.getProperty("user.dir").concat("/src/test/java") : group.getSourceDir();
        if (isNotBlank(group.getBasePackage())) {
            filePath.concat("/").concat(group.getBasePackage().replace(".", "/"));
        }

        return new File(filePath.concat("/").concat(templateConfig.buildClassName()).concat(".java"));
    }

    public static File buildXmlFile(TemplateConfig templateConfig) {
        GroupConfig group = templateConfig.getGroup();
        return new File(group.getBasePackage().replace(".", "/")
                .concat("/").concat(templateConfig.buildClassName()).concat(".xml"));
    }
}
